package deadLock;


import java.util.concurrent.TimeUnit;

public class DeadLockDemo {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockA){
                    System.out.println(Thread.currentThread().getName()+lockA+"want to get "+lockB);
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lockB){
                        System.out.println(Thread.currentThread().getName() + lockB + "want to get " + lockA);
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockB) {
                    System.out.println(Thread.currentThread().getName() + lockB + "want to get " + lockA);
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lockA) {
                        System.out.println(Thread.currentThread().getName() + lockA + "want to get " + lockB);
                    }
                }
            }
        }).start();
    }
}
